package com.kbtd.service.impl;

import com.kbtd.service.IEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class EmailServiceImpl implements IEmailService {

    private static final String SEND_NAME = "口碑探店";

    @Resource
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String fromEmail;

    @Override
    public void sendSimpleEmail(String to, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        String from = String.format("%s<%s>", SEND_NAME, fromEmail);
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        javaMailSender.send(message);
    }

    @Override
    public void sendCode(String to, String code) {
        String subject = "用户登录验证";
        String codeCommonText = "您的验证码为%s, 有效期10分钟，如非本人操作请忽略此邮件！";
        String text = String.format(codeCommonText, code);
        sendSimpleEmail(to, subject, text);
    }
}
